package com.nstar.carousel.itemrenderer
{
	import com.nstar.base.event.AdvanceEvent;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.events.FlexEvent;

	public class ImageThumbnail extends Canvas
	{
		public static const SELECT:String = "select";
		public static const UNSELECT:String = "unselect";
		
		public static const SELECTED_BORRDER_COLOR:uint = 0xFF0000;
		public static const UNSELECTED_BORRDER_COLOR:uint = 0x808080;
		public static const ROLLOVER_BORRDER_COLOR:uint = 0xFF0000;
		
		public static const SELECTED_ALPHA:Number = 1;
		public static const UNSELECTED_ALPHA:Number = 0.6;
		public static const ROLLOVER_ALPHA:Number = 0.8;
		
		private var selected:Boolean = false;
		private var image:Image;
		
		private var _product:Object;
		
		public function ImageThumbnail()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			this.setStyle("borderStyle","solid");
			this.setStyle("borderColor",UNSELECTED_BORRDER_COLOR);
			this.setStyle("backgroundColor",0xFFFFFF);
			this.setStyle("horizontalScrollPolicy","off");
			this.setStyle("verticalScrollPolicy","off");	
			
			
			this.addEventListener(SELECT,selectHandler);
			this.addEventListener(UNSELECT,unselectHandler);
			this.addEventListener(MouseEvent.ROLL_OVER,rollHandler);
			this.addEventListener(MouseEvent.ROLL_OUT,rollHandler);
			
			this.buttonMode = true;
			this.alpha = UNSELECTED_ALPHA;
			
			image = new Image();
			image.percentWidth = 100;
			image.percentHeight = 100;
			image.setStyle("horizontalAlign","center");
			image.setStyle("verticalAlign","middle");
			this.addChild(image);
			
			//绑定属性
			BindingUtils.bindProperty(image,"source",_product,"image");
		}
		
		public function set product(value:Object):void{
			this._product = value;
		}
		
		private function selectHandler(event:AdvanceEvent):void{
			this.setStyle("borderColor",SELECTED_BORRDER_COLOR);
			this.alpha = SELECTED_ALPHA;
			this.selected = true;
		}
		
		private function unselectHandler(event:AdvanceEvent):void{
			this.setStyle("borderColor",UNSELECTED_BORRDER_COLOR);
			this.alpha = UNSELECTED_ALPHA;
			this.selected = false;
		}
		
		private function rollHandler(event:MouseEvent):void{
			switch(event.type){
				case MouseEvent.ROLL_OVER:
					if(!selected){
						this.setStyle("borderColor",ROLLOVER_BORRDER_COLOR);
						this.alpha = ROLLOVER_ALPHA;
					}
					break;
				case MouseEvent.ROLL_OUT:
					if(!selected){
						this.setStyle("borderColor",UNSELECTED_BORRDER_COLOR);
						this.alpha = UNSELECTED_ALPHA;
					}
					break;
			}
		}
		
		override protected function createChildren():void{
			super.createChildren();
		}
	}
}